<?php
namespace app\admin\controller;

use app\common\cache\agent\IAgentAdminCache;
use app\common\cache\user\IUserCache;
use app\common\controller\AdminBaseController;
use app\common\enum\IsDelete;
use app\common\ext\IDb;
use app\common\ext\IRequest;
use think\Db;

class AgentQuestion extends AdminBaseController{

    /**
     * @功能：问题列表
     * @开发者：gys
     * @return string
     */
    public function question_list(){
        //接收参数
        $question_id = IRequest::get('question_id');
        $question_content = IRequest::get('question_content');
        $is_reply = IRequest::get('is_reply');

        if($is_reply == 1){
            $question_where['cq.reply_content'] = ['exp','is null'];
        }else if($is_reply == 2){
            $question_where['cq.reply_content'] = ['exp','is not null'];
        }
        //设置查询条件
        if(!empty($question_id)) $question_where['cq.question_id'] = $question_id;
        if(!empty($question_content)) $question_where['question_content'] = array("like","%{$question_content}%");
        $question_where['cq.is_delete'] = IsDelete::No;
        $question_list = IDb::getInstance('agent_question cq')
            ->setDbFiled("cq.*,ca.nick,ca.real")
            ->setDbJoin("user as ca","cq.user_id = ca.user_id","left")
            ->setDbWhere($question_where)
            ->setDbOrder("question_id desc")
            ->pag();

        //页面赋值
        $this->assign("question_id",$question_id);
        $this->assign("is_reply",$is_reply);
        $this->assign("question_content",$question_content);
        $this->assign("question_list",$question_list);

        //渲染模板输出
        return view();
    }

    /**
     * @功能：问答详情
     * @开发者：gys
     * @return string
     */
    public function question_info(){
        //接收参数
        $question_id = IRequest::get("question_id",IRequest::NOT_EMPTY,"问答编号不能为空！");

        //取得问答表
        $question_where['question_id'] = $question_id;
        $question_info = IDb::getInstance('agent_question')->setDbWhere($question_where)->row();

        $user_info = [];
        if(!empty($question_info['user_id'])){
            $user_info = IUserCache::getInfo($question_info['user_id']);
        }

        //设置layout
        $this->layout("layout_empty");

        //页面赋值
        $this->assign("question_info",$question_info);
        $this->assign("user_info",$user_info);

        //渲染模板输出
        return view();
    }

    /**
     * @功能：问答删除
     * @开发者：gys
     * @return string
     */
    public function question_del(){
        //接收参数
        $question_id = IRequest::get("question_id",IRequest::NOT_EMPTY,"问答编号不能为空！");

        //修改管理员信息
        $question_where['question_id'] = $question_id;
        $question_data['is_delete'] = IsDelete::Yes;
        $question_upd = IDb::getInstance('agent_question')->setDbWhere($question_where)->setDbData($question_data)->upd();
        if($question_upd === false){

            //错误信息
            error("删除失败！");
        }

        //画面跳转
        $this->success("操作成功！");
    }
}
